﻿Imports System.Data.SqlClient

Public Class FrmLoaiBD

    Private Sub loadData(ByVal strsql As String)
        Dim DA As New SqlDataAdapter(strsql, ob_cnn)
        Dim DS As New DataSet
        DA.Fill(DS)
        DataGridViewBD.DataSource = DS.Tables(0)
    End Sub
    Public Sub NapDanhSachVaoListBox()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select mabd, tenloaibd from dbo.LOAIBD", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        '   xuat_loai_dat()
        DataGridViewBD.DataSource = tbl
        ob_cnn.Close()
    End Sub

    Private Sub FrmLoaiBD_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NapDanhSachVaoListBox()
    End Sub

    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()

    End Sub
    Sub xuat_loai_BD()
        Dim dong_hien_hanh As Integer = DataGridViewBD.CurrentRow.Index
        TextID.Text = DataGridViewBD.Rows(dong_hien_hanh).Cells(0).Value
        TextLoaiBienDong.Text = DataGridViewBD.Rows(dong_hien_hanh).Cells(1).Value
    End Sub
    Private Sub ButtonThêm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThêm.Click
        Try
            '  If MsgBox("Bạn có muốn lưu không ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Luu thông ting ") = MsgBoxResult.Yes Then

            If TextID.Text = "" Or TextLoaiBienDong.Text = "" Then
                MsgBox(" Nhập thông tin không đầy đủ !!! ")
            Else
                Dim lenh As String = ""
                lenh = "insert into LOAIBD (mabd, tenLoaiBD )values('" & TextID.Text & "', N'" & TextLoaiBienDong.Text & "')"
                Dim cmd As New SqlCommand(lenh, ob_cnn)
                ob_cnn.Open()
                cmd.ExecuteNonQuery()
                ob_cnn.Close()
                loadData("select mabd, tenLoaiBD from LoaiBD  ") 'lam tuoi du lieu

                MsgBox("Thêm thông tin thành công!")
                ob_cnn.Close()
            End If
            '  End If
        Catch ex As Exception
            If ex.Message.Contains("duplicate values in the index, primary key, or relationship") Then
                MessageBox.Show("Trùng mã, nhập lại!")
                ob_cnn.Close()
            Else
                MessageBox.Show("Loại biến động đã trùng, nhập lại !!!")
                ob_cnn.Close()
            End If
        End Try

    End Sub

    Private Sub ButtonSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSua.Click

        Try
            If TextID.Text = "" Or TextLoaiBienDong.Text = "" Then
                MsgBox(" Nhập thông tin không đầy đủ !!! ")
            Else
                Dim lenh As String = ""
                lenh = "update LOAIBD set tenLoaiBD = N'" & TextLoaiBienDong.Text & "' where  mabd = '" & TextID.Text & "'"
                Dim cmd As New SqlCommand(lenh, ob_cnn)
                ob_cnn.Open()
                cmd.ExecuteNonQuery()
                ob_cnn.Close()
                loadData("select mabd, tenLoaiBD from LoaiBD ") 'lam tuoi du lieu
                MsgBox("Sửa thông tin thành công!")
                ob_cnn.Close()
            End If

        Catch ex As Exception
            If ex.Message.Contains("duplicate values in the index, primary key, or relationship") Then
                ob_cnn.Close()
            Else
                MessageBox.Show("Thông tin đã trùng, nhập lại !!!")
                ob_cnn.Close()
            End If
        End Try
    End Sub

    Private Sub DataGridViewBD_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewBD.CellClick
        xuat_loai_BD()
    End Sub

End Class